package com.taha.mail.controller;

/**
 * @author Bahare
 *
 */
import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taha.mail.entity.UserBean;
import com.taha.mail.exception.CreateAcountException;
import com.taha.mail.service.UserService;
import com.taha.mail.service.UserServiceImp;



public class CreateAccountController extends HttpServlet implements Servlet{
	
	
	UserService<UserBean> userservice;
	String firstname=null;
	String lastname=null;
	String username=null;
	String password=null;
	String reEnterPassword=null;
	String notfilled=null;
	String Notequal=null;
	String pass_TooShort=null;
	String registering_failed=null;
	String UserNameAvailability=null;
	
	public  CreateAccountController() {
 
		
	}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{   
	String action=request.getParameter("check");
	
    if(action==null){
    	
    	action=request.getParameter("create");
    }
    
      
     firstname=request.getParameter("firstname");
	 lastname=request.getParameter("lastname");
	 username=request.getParameter("username");
	 password=request.getParameter("password");
	 reEnterPassword=request.getParameter("reEnterpassword");
	 boolean is_inserted=false;
	 
	
	 
	//if check userNameAvailability button hit:
	 if(action.equals("check availability!")){
		 skipSpaceAndTab();
		 UserNameAvailability=checkUserNameAvailability(username);
		 notfilled=null;
		 Notequal=null;
		 pass_TooShort=null;
		 registering_failed=null;
		 setAttributes(request, response);

	    }
	 
	 // if CreateAcount button hit:
	 else{
	
	// check to see all the boxes are filled
	if(firstname=="" || lastname=="" || username=="" || password==""){
	 notfilled="please fill all the boxes";
	 Notequal=null;
	 pass_TooShort=null;
	 UserNameAvailability=null;
	 registering_failed=null;
	 setAttributes( request, response);
	}
	
	else {
		
		// check the length of password
		if(password.length() < 8)
		{
			pass_TooShort="minimum of 8 charachter in length";
			 Notequal=null;
			 notfilled=null;
			 UserNameAvailability=null;
			 registering_failed=null;
			 setAttributes(request, response);
		}
		else {
		//check to see password and re enter password are equal
	if(password.equals(reEnterPassword) ==false)
	{
		notfilled=null;
		 pass_TooShort=null;
		 Notequal="passwords don't match";
		 UserNameAvailability=null;
		 registering_failed=null;
		 setAttributes( request, response);
		
	}
	
	else{
	
	boolean is_available=userservice.checkAvailibility(username);
	if(is_available)
	{
	UserBean newUser=new UserBean(username, password, firstname, lastname);
	 try {
		is_inserted=userservice.CreateAcount(newUser);
	} catch (CreateAcountException e) {
		
		e.getMessage();
	}
	
	if(is_inserted)
	{
		request.getSession().setAttribute("firstname", firstname);
		request.getSession().setAttribute("laststname", lastname);
		getServletContext().getRequestDispatcher("/WEB-INF/jsp/CreateAccountSucceed.jsp").forward(request, response);
		
	}
	}
	else
	{
		 registering_failed=username+" has already been taken.\n"+"please choose another username";
		 notfilled=null;
		 pass_TooShort=null;
		 Notequal=null;
		 UserNameAvailability=null;
		
		setAttributes(request, response);
		
	}
	}
	}
	}
	
	 }//end outer else
	
}


protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
	doPost(request, response);
}

public void setAttributes(HttpServletRequest request,
		HttpServletResponse response)
throws ServletException,IOException
{
	 request.getSession().setAttribute("dontMatch", Notequal);
	 request.getSession().setAttribute("firstname", firstname);
	 request.getSession().setAttribute("lastname", lastname);
	 request.getSession().setAttribute("username", username);
	 request.getSession().setAttribute("notfilled", notfilled);
	 request.getSession().setAttribute("pass_TooShort", pass_TooShort);
	 request.getSession().setAttribute("registering_failed",registering_failed);
	 request.getSession().setAttribute("UserNameAvailability", UserNameAvailability);
	 
	 getServletContext().getRequestDispatcher("/WEB-INF/jsp/CreateAccount.jsp").forward(request, response);
}

private String checkUserNameAvailability(String username){
	
    if(username.equals("")){
    	UserNameAvailability="please enter username!";
    }
    else{
	
	    userservice=new UserServiceImp();
	    boolean isAvailable=userservice.checkAvailibility(username);		

		
	if(isAvailable)
		 UserNameAvailability="userName is Available.";
	else
		UserNameAvailability="userName is not Available.";
    }
	return UserNameAvailability;
	
}

//this method skips space and tab characters from  username
private void skipSpaceAndTab() {
	int i=0;
	if(!username.equals("")){
		
	for(i=0;i<username.length() && (username.charAt(i)==' ' || username.charAt(i)=='\t');i++);
	
	username=username.substring(i);	
	}
}
}
